package com.woniuxy.controller;

import com.woniuxy.entity.UserAddress;
import com.woniuxy.service.AddressService;
import com.woniuxy.util.ResponseData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Resource
    private AddressService addressService;

    @GetMapping("/queryAddressByUserId")
    private ResponseData queryAddressByUserId() throws ParseException { //按照用户id查找对应的多地址
        List<UserAddress> userAddressList = addressService.queryAddressByUserId();
        return ResponseData.success(userAddressList);
    }

    @PostMapping("/addAddress")
    public ResponseData addAddress(@RequestBody UserAddress userAddress) throws ParseException { //添加地址
        addressService.addAddress(userAddress);
        return ResponseData.success(null);
    }

    @PostMapping("/updateAddress")
    public ResponseData updateAddress(@RequestBody UserAddress userAddress) throws ParseException { //修改地址
        addressService.updateAddress(userAddress);
        return ResponseData.success(null);
    }

    @GetMapping("/deleteAddress/{id}") //按照地址id删除地址
    private ResponseData deleteAddress(@PathVariable("id") Integer id) {
        addressService.deleteAddress(id);
        return ResponseData.success(null);
    }

    @GetMapping("/changeDefaultAddress/{id}") // 按照地址id修改用户默认地址, id为要成为默认地址的id
    private ResponseData changeDefaultAddress(@PathVariable("id") Integer id) throws ParseException {
        addressService.changeDefaultAddress(id);
        return ResponseData.success(null);
    }

    @GetMapping("/queryAddressById/{id}") // 按照地址id查找一个地址，作用是能回显给前端要修改的指定地址
    private ResponseData queryAddressById(@PathVariable("id") Integer id) {
        UserAddress userAddress = addressService.queryAddressById(id);
        return ResponseData.success(userAddress);
    }
}
